Parameter substitution in named routes and generate URLs dynamically, including handling optional parameters and generating URLs in views and redirects.
// Define named routes with optional parameters
Route::get('post/{post}/comment/{comment?}', [CommentController::class, 'show'])
->name('post.comment');
// Generate URL with required and optional parameters
$url = route('post.comment', ['post' => 1, 'comment' => 5]);
// Generate URL with only required parameters
$url = route('post.comment', ['post' => 1]);
// Redirect to a named route with parameters
return redirect()->route('post.comment', ['post' => 1, 'comment' => 5]);
Defines a named route 'post.comment' with an optional {comment} parameter. If {comment} is not provided, the route will still be valid.Route::get('post/{post}/comment/{comment?}', [CommentController::class, 'show'])->name('post.comment'):
Generates a URL for the 'post.comment' route with both required and optional parameters substituted.route('post.comment', ['post' => 1, 'comment' => 5]):
Generates a URL for the 'post.comment' route with only the required parameter.route('post.comment', ['post' => 1]):
Redirects to the named route with specified parameters.redirect()->route('post.comment', ['post' => 1, 'comment' => 5]):
You Might Also Like
Reduce Template Size with Blade Includes
Description: Break down large Blade templates into smaller reusable components using @include direct...
Modify Response with Middleware
Modify or enhance responses using middleware, such as adding headers, manipulating content, or handl...